//
//  CellForViewController.m
//  SeeTheWorld
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CellForViewController.h"
#import "CellTableViewForYouJi.h"
#import "CellForScrollView.h"
#import "NetworkingHandler.h"
#import "ModelOfScrollView.h"
#import "ModelOfShouYeCell.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "PCH.pch"
#import <UIImageView+WebCache.h>


#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define HEADHEIGHT 64
#define FOOTHEIGHT 49

@interface CellForViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arrForScrollView;
@property (nonatomic, retain)NSMutableArray *arrForShouYeCell;
@property (nonatomic, assign)BOOL isHave;
@property (nonatomic, assign)BOOL isSecond;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, retain)NSString *url;
@property (nonatomic, retain)MBProgressHUD *hub;

@end

@implementation CellForViewController

- (void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [_hub release];
    [_url release];
    [_arrForShouYeCell release];
    [_arrForScrollView release];
    [_tableView release];
    [super dealloc];
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
 
        self.isHave = NO;
        self.isSecond = NO;
    
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.contentView.frame.size.height)];
        [self.contentView addSubview:self.tableView];
        [_tableView release];
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        self.tableView.tableHeaderView.backgroundColor = [UIColor redColor];
        self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

        [self.tableView registerClass:[CellTableViewForYouJi class] forCellReuseIdentifier:@"pool1"];
        [self.tableView registerClass:[CellForScrollView class] forCellReuseIdentifier:@"pool2"];
        
        
        
        /** 解析轮播图数据 */
        [NetworkingHandler networkingWithURL:@"http://open.qyer.com/qyer/recommands/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204S&track_deviceid=837CADE8-B56C-44A5-9B68-053B33D49677&track_os=ios%25207.1&v=1" completeHandle:^(NSData *data,id result) {
            
            self.arrForScrollView = [NSMutableArray array];
            NSArray *array = [[result objectForKey:@"data"] objectForKey:@"slide"];
            
            for (NSDictionary *dic in array) {
                ModelOfScrollView *model = [[ModelOfScrollView alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.arrForScrollView addObject:model];
                [model release];
            }
            [self.tableView reloadData];
        }];
        
        /** 解析首页游记cell数据 */
        [self handleData];
    

        
#pragma mark - 下拉刷新
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
           
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [NetworkingHandler networkingWithURL:@"http://open.qyer.com/qyer/recommands/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204S&track_deviceid=837CADE8-B56C-44A5-9B68-053B33D49677&track_os=ios%25207.1&v=1" completeHandle:^(NSData *data,id result) {
                    self.arrForScrollView = [NSMutableArray array];
                    NSArray *array = [[result objectForKey:@"data"] objectForKey:@"slide"];
                    
                    for (NSDictionary *dic in array) {
                        ModelOfScrollView *model = [[ModelOfScrollView alloc] init];
                        [model setValuesForKeysWithDictionary:dic];
                        [self.arrForScrollView addObject:model];
                        [model release];
                    }
                    [self.tableView reloadData];
                    [self.tableView.mj_header endRefreshing];
                }];
                
                [self handleData];
                
            });
            
        }];
        
    }
    
    return self;
}

#pragma mark - 解析数据
- (void)handleData {
    
    self.page = 1;
    self.url = [NSString stringWithFormat:@"http://chanyouji.com/api/trips/featured.json?page=%ld", (long)self.page];
    
    /** 解析首页tableViewCell数据 */
    [NetworkingHandler networkingWithURL:self.url completeHandle:^(NSData *data,id result) {
        
        self.arrForShouYeCell = [NSMutableArray array];
        
        
        for (NSDictionary *dic in result) {
            ModelOfShouYeCell *model = [[ModelOfShouYeCell alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForShouYeCell addObject:model];
            [model release];
            
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
#pragma mark - 上拉加载
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            self.page = self.page + 1;
            self.url = [NSString stringWithFormat:@"http://chanyouji.com/api/trips/featured.json?page=%ld", (long)self.page];
            
            [NetworkingHandler networkingWithURL:self.url completeHandle:^(NSData *data,id result) {
                
                for (NSDictionary *dic in result) {
                    
                    ModelOfShouYeCell *model = [[ModelOfShouYeCell alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.arrForShouYeCell addObject:model];
                    [model release];
                }
                [self.tableView reloadData];
                [self.hub hide:YES];
                
            }];
            
            [self.tableView.mj_footer endRefreshing];
            
        }];
    }];
    
}

#pragma mark - tableView协议方法
/** 返回每行cell的高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 220;
    } else {
        return 200;
    }
}

/** 返回cell个数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrForShouYeCell.count;
}

/** 返回cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        CellForScrollView *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

        if (self.isHave == NO) {
            
            [cell passModel:self.arrForScrollView];
            
            if (self.isSecond == YES) {
                
                self.isHave = YES;
            }
            self.isSecond = YES;
        }
       
        return cell;
    } else {
        CellTableViewForYouJi *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        
        ModelOfShouYeCell *model = [self.arrForShouYeCell objectAtIndex:indexPath.row - 1];
        [cell passModel:model];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
 
        return cell;
    }
}

/** 点击cell */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ModelOfShouYeCell *model = self.arrForShouYeCell[indexPath.row - 1];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passIndexPath" object:nil userInfo:@{@"content" : indexPath, @"model" : model.id}];
}


#pragma mark - 子控件布局
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    self.tableView.frame = CGRectMake(0, 0, WIDTH, self.contentView.frame.size.height);
    
}

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    return layoutAttributes;
}

- (void)viewWillAppear:(BOOL)animated {
    
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    [self.tableView reloadData];
    
}








@end
